package com.softjourn.punchcard.util;

import java.util.HashMap;
import java.util.UUID;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.softjourn.punchcard.activity.LoginActivity;

public class SessionManager {
	// Shared Preferences
	SharedPreferences pref;

	// Editor for Shared preferences
	Editor editor;

	// Context
	Context context;

	// Shared pref mode
	int PRIVATE_MODE = 0;

	// Sharedpref file name
	private static final String PREF_NAME = "PunchCardPref";

	// All Shared Preferences Keys
	private static final String IS_LOGIN = "isLoggedIn";
	private static final String IS_FIRST_LAUNCH = "isFirstLaunch";
	public static final String KEY_NAME = "name";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_UID = "uid";
	public static final String KEY_DEVICE_ID = "deviceId";
	public static final String KEY_API_URL = "apiUrl";
	
	// Constructor
	public SessionManager(Context context) {
		this.context = context;
		pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
		editor = pref.edit();
	}

	/**
	 * Create login session
	 * */
	public void createLoginSession(String name, String uid) {
		// Storing login value as TRUE
		editor.putBoolean(IS_LOGIN, true);

		// Storing name in pref
		editor.putString(KEY_NAME, name);

/*		// Storing email in pref
		editor.putString(KEY_EMAIL, email);*/
		
		// Storing email in pref
		editor.putString(KEY_UID, uid);

		// commit changes
		editor.commit();
	}

	/**
	 * Check login method wil check user login status If false it will redirect
	 * user to login page Else won't do anything
	 * */
	public boolean checkLogin() {
		// Check login status
		boolean test = isLoggedIn(); 
		if (!test) {
			// user is not logged in redirect him to Login Activity
			Intent i = new Intent(context, LoginActivity.class);
			// Closing all the Activities
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// Add new Flag to start new Activity
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// Staring Login Activity
			context.startActivity(i);
		}
		return test;
	}

	/**
	 * Get stored session data
	 * */
	public HashMap<String, String> getUserDetails() {
		HashMap<String, String> userDetails = new HashMap<String, String>();
		// user name
		userDetails.put(KEY_NAME, pref.getString(KEY_NAME, null));
		// user email id
		userDetails.put(KEY_UID, pref.getString(KEY_UID, null));
		// return user
		return userDetails;
	}
	
	/**
	 * Get stored session data
	 * */
	public HashMap<String, String> getSystemDetails() {
		HashMap<String, String> systemDetails = new HashMap<String, String>();
		// api URL
		systemDetails.put(KEY_API_URL, pref.getString(KEY_API_URL, null));
		// device id
		systemDetails.put(KEY_DEVICE_ID, pref.getString(KEY_DEVICE_ID, null));

		// return sysytem
		return systemDetails;
	}
	

	/**
	 * Clear session details
	 * */
	public void logoutUser() {
		// Clearing all data from Shared Preferences
		clearSessionData();
		// After logout redirect user to Loing Activity
		Intent i = new Intent(context, LoginActivity.class);
		// Closing all the Activities
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// Add new Flag to start new Activity
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// Staring Login Activity
		context.startActivity(i);
	}

	public void clearSessionData() {
		editor.clear();
		editor.commit();
	}
	
	/**
	 * Quick check for login
	 * **/
	// Get Login State
	public boolean isLoggedIn() {
		Log.i("PREF",  ""+pref.getBoolean(IS_LOGIN, false));
		return pref.getBoolean(IS_LOGIN, false);
	}
	
	public void checkFirstLaunch() {
		if(pref.getBoolean(IS_FIRST_LAUNCH, true)) {
			editor.putString(KEY_DEVICE_ID, generateDeviceId());
			editor.putString(KEY_API_URL, generateDeviceId());
		}
	}
	
	
	private String generateDeviceId() {
	    final String macAddr, androidId;

	    WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	    WifiInfo wifiInf = wifiMan.getConnectionInfo();

	    macAddr = "" + wifiInf.getMacAddress();
	    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), 
	    		android.provider.Settings.Secure.ANDROID_ID);

	    UUID deviceUuid = new UUID(androidId.hashCode(), macAddr.hashCode());

	    return deviceUuid.toString();
	}	

}